home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / mkinitramfs < prev    next >
Encoding:
Text File  |  2012-09-21  |  9.0 KB  |  375 lines

  1. #!/bin/sh
  2.  
  3. umask 0022
  4. export PATH='/usr/bin:/sbin:/bin'
  5.  
  6. # Defaults
  7. keep="n"
  8. CONFDIR="/etc/initramfs-tools"
  9. verbose="n"
  10. test -e /bin/busybox && BUSYBOXDIR=/bin
  11. test -e /usr/lib/initramfs-tools/bin/busybox && BUSYBOXDIR=/usr/lib/initramfs-tools/bin
  12. export BUSYBOXDIR
  13.  
  14. OPTIONS=`getopt -o c:d:ko:r:v -n "$0" -- "$@"`
  15.  
  16. # Check for non-GNU getopt
  17. if [ $? != 0 ] ; then echo "W: non-GNU getopt" >&2 ; exit 1 ; fi
  18.  
  19. eval set -- "$OPTIONS"
  20.  
  21. while true; do
  22.     case "$1" in
  23.     -c)
  24.         compress="$2"
  25.         shift 2
  26.         ;;
  27.     -d)
  28.         CONFDIR="$2"
  29.         shift 2
  30.         if [ ! -d "${CONFDIR}" ]; then
  31.             echo "${0}: ${CONFDIR}: Not a directory" >&2
  32.             exit 1
  33.         fi
  34.         ;;
  35.     -o)
  36.         outfile="$2"
  37.         shift 2
  38.         ;;
  39.     -k)
  40.         keep="y"
  41.         shift
  42.         ;;
  43.     -r)
  44.         ROOT="$2"
  45.         shift 2
  46.         ;;
  47.     -v)
  48.         verbose="y"
  49.         shift
  50.         ;;
  51.     --)
  52.         shift
  53.         break
  54.         ;;
  55.     *)
  56.         echo "Internal error!" >&2
  57.         exit 1
  58.         ;;
  59.     esac
  60. done
  61.  
  62. # For dependency ordered mkinitramfs hook scripts.
  63. . /usr/share/initramfs-tools/scripts/functions
  64. . /usr/share/initramfs-tools/hook-functions
  65.  
  66. . "${CONFDIR}/initramfs.conf"
  67. EXTRA_CONF=''
  68. for i in /usr/share/initramfs-tools/conf.d/* ${CONFDIR}/conf.d/*; do
  69.     [ -e $i ] && EXTRA_CONF="${EXTRA_CONF} $(basename $i \
  70.         | grep '^[[:alnum:]][[:alnum:]\._-]*$' | grep -v '\.dpkg-.*$')";
  71. done
  72. # FIXME: deprecated those settings on mkinitramfs run
  73. #      these conf dirs are for boot scripts and land on initramfs
  74. for i in ${EXTRA_CONF}; do
  75.     if [ -d  ${CONFDIR}/conf.d/${i} ]; then
  76.         echo "Warning: ${CONFDIR}/conf.d/${i} is a directory instead of file, ignoring."
  77.     elif [ -e  ${CONFDIR}/conf.d/${i} ]; then
  78.         . ${CONFDIR}/conf.d/${i}
  79.     elif [ -e  /usr/share/initramfs-tools/conf.d/${i} ]; then
  80.         . /usr/share/initramfs-tools/conf.d/${i}
  81.     fi
  82. done
  83.  
  84. # source package confs
  85. for i in /usr/share/initramfs-tools/conf-hooks.d/*; do
  86.     if [ -d "${i}" ]; then
  87.         echo "Warning: ${i} is a directory instead of file, ignoring."
  88.     elif [ -e "${i}" ]; then
  89.         . "${i}"
  90.     fi
  91. done
  92.  
  93. if [ -n "${UMASK:-}" ]; then
  94.     umask "${UMASK}"
  95. fi
  96.  
  97. if [ -z "${outfile}" ]; then
  98.     usage
  99. fi
  100.  
  101. touch "$outfile"
  102. outfile="$(readlink -f "$outfile")"
  103.  
  104. # And by "version" we really mean path to kernel modules
  105. # This is braindead, and exists to preserve the interface with mkinitrd
  106. if [ ${#} -ne 1 ]; then
  107.     version="$(uname -r)"
  108. else
  109.     version="${1}"
  110. fi
  111.  
  112. case "${version}" in
  113. /lib/modules/*/[!/]*)
  114.     ;;
  115. /lib/modules/[!/]*)
  116.     version="${version#/lib/modules/}"
  117.     version="${version%%/*}"
  118.     ;;
  119. esac
  120.  
  121. case "${version}" in
  122. */*)
  123.     echo "$PROG: ${version} is not a valid kernel version" >&2
  124.     exit 1
  125.     ;;
  126. esac
  127.  
  128. # Check userspace and kernel support for compressed initramfs images
  129. if [ -z "${compress:-}" ]; then
  130.     compress=${COMPRESS}
  131. else
  132.     COMPRESS=${compress}
  133. fi
  134.  
  135. if ! command -v "${compress}" >/dev/null 2>&1; then
  136.     compress=gzip
  137.     [ "${verbose}" = y ] && \
  138.         echo "No ${COMPRESS} in ${PATH}, using gzip"
  139.     COMPRESS=gzip
  140. fi
  141.  
  142. if dpkg --compare-versions "${version}" lt "2.6.38" 2>/dev/null; then
  143.     compress=gzip
  144.     [ "${verbose}" = y ] && \
  145.         echo "linux-2.6 likely misses ${COMPRESS} support, using gzip"
  146. fi
  147.  
  148. [ "${compress}" = lzop ] && compress="lzop -9"
  149. [ "${compress}" = xz ] && compress="xz -8 --check=crc32"
  150.  
  151. if [ -d "${outfile}" ]; then
  152.     echo "${outfile} is a directory" >&2
  153.     exit 1
  154. fi
  155.  
  156. MODULESDIR="/lib/modules/${version}"
  157.  
  158. if [ ! -e "${MODULESDIR}" ]; then
  159.     echo "WARNING: missing ${MODULESDIR}"
  160.     echo "Device driver support needs thus be built-in linux image!"
  161. fi
  162. if [ ! -e "${MODULESDIR}/modules.dep" ]; then
  163.     depmod ${version}
  164. fi
  165.  
  166. [ -n "${TMPDIR}" ] && [ ! -w "${TMPDIR}" ] && unset TMPDIR
  167. DESTDIR="$(mktemp -d ${TMPDIR:-/var/tmp}/mkinitramfs_XXXXXX)" || exit 1
  168. chmod 755 "${DESTDIR}"
  169.  
  170. # do not execute cache_run_scripts() if mounted with noexec
  171. NOEXEC=""
  172. fs=$(df -P $DESTDIR | tail -1 | awk '{print $6}')
  173. if [ -n "$fs" ] && mount | grep -q "on $fs .*noexec" ; then
  174.     NOEXEC=1
  175. fi
  176.  
  177. __TMPCPIOGZ="$(mktemp ${TMPDIR:-/var/tmp}/mkinitramfs-OL_XXXXXX)" || exit 1
  178.  
  179. DPKG_ARCH=`dpkg --print-architecture`
  180.  
  181. # Export environment for hook scripts.
  182. #
  183. export MODULESDIR
  184. export version
  185. export CONFDIR
  186. export DESTDIR
  187. export DPKG_ARCH
  188. export verbose
  189. export KEYMAP
  190. export MODULES
  191. export BUSYBOX
  192.  
  193. # Private, used by 'catenate_cpiogz'.
  194. export __TMPCPIOGZ
  195.  
  196. for d in bin conf/conf.d etc lib/modules run sbin scripts ${MODULESDIR}; do
  197.     mkdir -p "${DESTDIR}/${d}"
  198. done
  199.  
  200. # Copy in modules.builtin and modules.order (not generated by depmod)
  201. for x in modules.builtin modules.order; do
  202.     if [ -f "${MODULESDIR}/${x}" ]; then
  203.         cp -p "${MODULESDIR}/${x}" "${DESTDIR}${MODULESDIR}/${x}"
  204.     fi
  205. done
  206.  
  207. # MODULES=list case.  Always honour.
  208. for x in "${CONFDIR}/modules" /usr/share/initramfs-tools/modules.d/*; do
  209.     if [ -f "${x}" ]; then
  210.         add_modules_from_file "${x}"
  211.     fi
  212. done
  213.  
  214. # MODULES=most is default
  215. case "${MODULES}" in
  216. dep)
  217.     dep_add_modules
  218.     ;;
  219. most)
  220.     auto_add_modules
  221.     ;;
  222. netboot)
  223.     auto_add_modules base
  224.     auto_add_modules net
  225.     ;;
  226. list)
  227.     # nothing to add
  228.     ;;
  229. *)
  230.     echo "W: mkinitramfs: unsupported MODULES setting: ${MODULES}."
  231.     echo "W: mkinitramfs: Falling back to MODULES=most."
  232.     auto_add_modules
  233.     ;;
  234. esac
  235.  
  236. # Resolve hidden dependencies
  237. hidden_dep_add_modules
  238.  
  239. # First file executed by linux-2.6
  240. cp -p /usr/share/initramfs-tools/init ${DESTDIR}/init
  241.  
  242. # add existant boot scripts
  243. for b in $(cd /usr/share/initramfs-tools/scripts/ && find . \
  244.     -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f); do
  245.     [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
  246.         || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
  247.     cp -p "/usr/share/initramfs-tools/scripts/${b}" \
  248.         "${DESTDIR}/scripts/$(dirname "${b}")/"
  249. done
  250. for b in $(cd "${CONFDIR}/scripts" && find . \
  251.     -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f); do
  252.     [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
  253.         || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
  254.     cp -p "${CONFDIR}/scripts/${b}" "${DESTDIR}/scripts/$(dirname "${b}")/"
  255. done
  256.  
  257. echo "DPKG_ARCH=${DPKG_ARCH}" > ${DESTDIR}/conf/arch.conf
  258. cp -p "${CONFDIR}/initramfs.conf" ${DESTDIR}/conf
  259. for i in ${EXTRA_CONF}; do
  260.     if [ -e "${CONFDIR}/conf.d/${i}" ]; then
  261.         copy_exec "${CONFDIR}/conf.d/${i}" /conf/conf.d
  262.     elif [ -e "/usr/share/initramfs-tools/conf.d/${i}" ]; then
  263.         copy_exec "/usr/share/initramfs-tools/conf.d/${i}" /conf/conf.d
  264.     fi
  265. done
  266.  
  267. # ROOT hardcoding
  268. if [ -n "${ROOT:-}" ]; then
  269.     echo "ROOT=${ROOT}" > ${DESTDIR}/conf/conf.d/root
  270. fi
  271.  
  272. if ! command -v ldd >/dev/null 2>&1 ; then
  273.     echo "WARNING: no ldd around - install libc-bin" >&2
  274.     exit 1
  275. fi
  276.  
  277. # module-init-tools
  278. copy_exec /sbin/modprobe /sbin
  279. copy_exec /sbin/rmmod /sbin
  280. mkdir -p "${DESTDIR}/etc/modprobe.d"
  281. cp -a /etc/modprobe.d/* "${DESTDIR}/etc/modprobe.d/"
  282.  
  283. # workaround: libgcc always needed on old-abi arm
  284. if [ "$DPKG_ARCH" = arm ] || [ "$DPKG_ARCH" = armeb ]; then
  285.     cp -a /lib/libgcc_s.so.1 "${DESTDIR}/lib/"
  286. fi
  287.  
  288. run_scripts /usr/share/initramfs-tools/hooks
  289. run_scripts "${CONFDIR}"/hooks
  290.  
  291. # cache boot run order
  292. if [ -n "$NOEXEC" ]; then
  293.     echo "W: TMPDIR is mounted noexec, will not cache run scripts."
  294. else
  295.     for b in $(cd "${DESTDIR}/scripts" && find . -mindepth 1 -type d); do
  296.         cache_run_scripts "${DESTDIR}" "/scripts/${b#./}"
  297.     done
  298. fi
  299.  
  300. # generate module deps
  301. depmod -a -b "${DESTDIR}" ${version}
  302. rm -f "${DESTDIR}/lib/modules/${version}"/modules.*map
  303.  
  304. # make sure that library search path is up to date
  305. cp -ar /etc/ld.so.conf* "$DESTDIR"/etc/
  306. if ! ldconfig -r "$DESTDIR" ; then
  307.     [ $(id -u) != "0" ] \
  308.     && echo "ldconfig might need uid=0 (root) for chroot()" >&2
  309. fi
  310.  
  311. # Apply DSDT to initramfs
  312. if [ -e "${CONFDIR}/DSDT.aml" ]; then
  313.     copy_exec "${CONFDIR}/DSDT.aml" /
  314. fi
  315.  
  316. # Remove any looping or broken symbolic links, since they break cpio.
  317. [ "${verbose}" = y ] && xargs_verbose="-t"
  318. (cd "${DESTDIR}" && find . -type l -printf '%p %Y\n' | sed -n 's/ [LN]$//p' \
  319.     | xargs ${xargs_verbose:-} -rL1 rm -f)
  320.  
  321. # dirty hack for armhf's double-linker situation; if we have one of
  322. # the two known eglibc linkers, nuke both and re-create sanity
  323. if [ "$DPKG_ARCH" = armhf ]; then
  324.     if [ -e "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3" ] || \
  325.        [ -e "${DESTDIR}/lib/ld-linux-armhf.so.3" ]; then
  326.         rm -f "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
  327.         rm -f "${DESTDIR}/lib/ld-linux-armhf.so.3"
  328.         cp -aL /lib/ld-linux-armhf.so.3 "${DESTDIR}/lib/"
  329.         ln -sf /lib/ld-linux-armhf.so.3 "${DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
  330.     fi
  331. fi
  332.  
  333. [ "${verbose}" = y ] && echo "Building cpio ${outfile} initramfs"
  334. (
  335. # work around lack of "set -o pipefail" for the following pipe:
  336. # cd "${DESTDIR}" && find . | cpio --quiet -R 0:0 -o -H newc | gzip >"${outfile}" || exit 1
  337. exec 3>&1
  338. eval `
  339.     # http://cfaj.freeshell.org/shell/cus-faq-2.html
  340.     exec 4>&1 >&3 3>&-
  341.     cd  "${DESTDIR}"
  342.     {
  343.         find . 4>&-; echo "ec1=$?;" >&4
  344.     } | {
  345.         cpio --quiet -R 0:0 -o -H newc 4>&-; echo "ec2=$?;" >&4
  346.     } | ${compress} >"${outfile}"
  347.     echo "ec3=$?;" >&4
  348. `
  349. if [ "$ec1" -ne 0 ]; then
  350.     echo "E: mkinitramfs failure find $ec1 cpio $ec2 $compress $ec3"
  351.     exit "$ec1"
  352. fi
  353. if [ "$ec2" -ne 0 ]; then
  354.     echo "E: mkinitramfs failure cpio $ec2 $compress $ec3"
  355.     exit "$ec2"
  356. fi
  357. if [ "$ec3" -ne 0 ]; then
  358.     echo "E: mkinitramfs failure $compress $ec3"
  359.     exit "$ec3"
  360. fi
  361. ) || exit 1
  362.  
  363. if [ -s "${__TMPCPIOGZ}" ]; then
  364.     cat "${__TMPCPIOGZ}" >>"${outfile}" || exit 1
  365. fi
  366.  
  367. if [ "${keep}" = "y" ]; then
  368.     echo "Working files in ${DESTDIR} and overlay in ${__TMPCPIOGZ}"
  369. else
  370.     rm -rf "${DESTDIR}"
  371.     rm -rf "${__TMPCPIOGZ}"
  372. fi
  373.  
  374. exit 0
  375.